package cn.edu.nju.notification;

import java.util.List;

import cn.edu.nju.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NotificationActivity extends Activity {
	public static final String SYSTEM_NOTIFICATION_POSITION = "system_notification_position";
	public static final String AT_NOTIFICATION_POSITION = "at_notification_position";
	private NotificationProvider provider;
	private TextView titleText;
	private List<AtNotification> atNotifications;
	private List<SystemNotification> systemNotifications;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);
        
        provider = NotificationProvider.getInstance(this);
        provider.refresh();
        
        // init widget
        initTitleText();
        initRefreshButton();
        initSystemNotificationSection();
        initAtNotificationSection();
	}
	
	private void initTitleText(){
		titleText = (TextView) findViewById(R.id.notification_title_text);
		String text = getString(R.string.notification_page_text);
		titleText.setText(text);
	}
	
	private void initRefreshButton(){
		Button refreshButton = (Button) findViewById(R.id.refresh_notification_button);
		refreshButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				provider.refresh();
				initSystemNotificationSection();
				initAtNotificationSection();
			}
		});
	}
	
	private void initSystemNotificationSection(){
		systemNotifications = provider.getSystemNotifications();
		initSystemNotificationHeader();
		initSystemNotificationList();
	}
	
	private void initSystemNotificationHeader(){
		TextView titleText = (TextView) findViewById(R.id.system_notification_list_title);
		String title = getString(R.string.system_notification);
		title = title + "(" + systemNotifications.size() + ")";
		titleText.setText(title);
		
		Button clearButton = (Button) findViewById(R.id.clear_system_notification_button);
		clearButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				provider.clearSystemNotification();
				initSystemNotificationSection();
			}
		});
	}
	
	private void initSystemNotificationList(){
		LinearLayout viewGroup = (LinearLayout) findViewById(R.id.system_notification_list);
		viewGroup.removeAllViews();
		
		LayoutInflater inflater = getLayoutInflater();
		int notificationNumber = systemNotifications.size();
		for(int i = 0; i < notificationNumber; i++){
			View view = inflater.inflate(R.layout.notification_item, null);
			
			TextView contentText = (TextView) view.findViewById(R.id.notification_content_text);
			contentText.setText(systemNotifications.get(i).getSummary());
			
			view.setTag(R.id.VIEW_POSITION_TAG, i);
			view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					int position = (Integer) v.getTag(R.id.VIEW_POSITION_TAG);
					Intent intent = new Intent(NotificationActivity.this, SystemNotificationActivity.class);
					intent.putExtra(SYSTEM_NOTIFICATION_POSITION, position);
					startActivity(intent);
				}
			});
			
			viewGroup.addView(view);
			if(i < notificationNumber - 1){
				inflater.inflate(R.layout.line_separator, viewGroup);
			}
		}
	}
	
    private void initAtNotificationSection(){
    	atNotifications = provider.getAtNotifications();
    	initAtNotificationHeader();
    	initAtNotificationList();
    }
    
    private void initAtNotificationHeader(){
    	TextView titleText = (TextView) findViewById(R.id.at_notification_list_title);
		String title = getString(R.string.at_notification);
		title = title + "(" + atNotifications.size() + ")";
		titleText.setText(title);
		
		Button clearButton = (Button) findViewById(R.id.clear_at_notification_button);
		clearButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				provider.clearAtNotification();
				initAtNotificationSection();
			}
		});
    }
    
    private void initAtNotificationList(){
    	LinearLayout viewGroup = (LinearLayout) findViewById(R.id.at_notification_list);
		viewGroup.removeAllViews();
		
		LayoutInflater inflater = getLayoutInflater();
		int notificationNumber = atNotifications.size();
		for(int i = 0; i < notificationNumber; i++){
			View view = inflater.inflate(R.layout.notification_item, null);
			
			TextView contentText = (TextView) view.findViewById(R.id.notification_content_text);
			contentText.setText(atNotifications.get(i).getSummary());
			
			view.setTag(R.id.VIEW_POSITION_TAG, i);
			view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					int position = (Integer) v.getTag(R.id.VIEW_POSITION_TAG);
					Intent intent = new Intent(NotificationActivity.this, AtNotificationActivity.class);
					intent.putExtra(AT_NOTIFICATION_POSITION, position);
					startActivity(intent);
				}
			});
			
			viewGroup.addView(view);
			if(i < notificationNumber - 1){
				inflater.inflate(R.layout.line_separator, viewGroup);
			}
		}
    }
}
